package com.example.communitypro.controller;

import com.example.communitypro.CommunityproApplication;
import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/file")
@Api(tags="Upload")

public class UploadController {


    //图片上传测试
    @ResponseBody
    @RequestMapping("upload")
    @CrossOrigin
    public Map upload(MultipartFile file, CommunityInformation communityInformation,HttpServletRequest request){

        String communityMember = request.getParameter("communityMember");
        String communityName = request.getParameter("communityName");

        String prefix="";
        String dateStr="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = simpleDateFormat.format(date);
                String filepath="";
                if(CommunityproApplication.isWindows) {
                     filepath = "C:\\images\\" + dateStr + "\\" + uuid + "." + prefix;
                }
                else
                     filepath = "/home/atipiu/images/" + dateStr + "/" + uuid + "." + prefix;

                communityInformation.setCommunityLcon(filepath);

                File files=new File(filepath);
                //打印查看上传路径
                System.out.println(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map<String,Object> map2=new HashMap<>();
                Map<String,Object> map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("data",map2);
                map2.put("src","/images/"+ dateStr+"/"+uuid+"." + prefix);
                return map;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");



        return map;

    }


    @ResponseBody
    @RequestMapping("/uploadPostPhoto")
    @CrossOrigin
    @ApiOperation(value = "上传图片")

    public Result uploadPostPhoto(@RequestParam("file") MultipartFile file ){

        if (file.isEmpty())
            return Result.error().message("图片为空");

        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath="";
        if(CommunityproApplication.isWindows){
            filePath = "C:\\image\\"; // 上传后的路径
        }else filePath="/home/atipiu/images/";
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            String filename = "/image/" + fileName;
            System.out.println(fileName);
            return Result.ok().message("上传图片成功").data("file",filename);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("上传图片失败"+e.toString());
        }
    }

    @ResponseBody
    @RequestMapping("/deletePostPhoto")
    @CrossOrigin
    public Result deletePostPhoto(String file){

        String filePath="";
        if(CommunityproApplication.isWindows){
            filePath = "C:\\image\\"; // 上传后的路径
        }else filePath="/home/atipiu/images/";
        file = filePath + file; // 新文件名
        File file1 = new File(file);
        try {
            boolean flag = file1.delete(); // 删除照片
            if (flag) {
                return Result.ok().message("删除图片成功");
            } else {
                return Result.error().message("删除图片成功");
            }
        }catch (Exception e){
            return Result.error().message("上传图片失败");
        }
    }
}
